/*
Copyright 2010 Johan Maasing

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package nu.zoom.android.gdata.project.integration;

import java.io.IOException;

import nu.zoom.android.gdata.project.client.WKC;
import android.net.Uri;
import android.util.Log;

import com.google.api.client.googleapis.GoogleUrl;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.apache.ApacheHttpTransport;
import com.google.api.client.http.xml.atom.AtomParser;
import com.google.api.client.xml.XmlNamespaceDictionary;

/**
 * @author Johan Maasing
 * 
 */
public class ProjectHostingNetClient implements ProjectHostingClient {
	private static final String LOG_TAG = WKC.LOG_TAG;
	private String authToken = null;
	private final HttpRequestFactory httpRequestFactory;

	private static final XmlNamespaceDictionary NAMESPACE_DICTIONARY = new XmlNamespaceDictionary()
			.set("", "http://www.w3.org/2005/Atom")
			// .set("atom", "http://www.w3.org/2005/Atom")
			.set("exif", "http://schemas.google.com/photos/exif/2007").set("gd", "http://schemas.google.com/g/2005")
			.set("geo", "http://www.w3.org/2003/01/geo/wgs84_pos#").set("georss", "http://www.georss.org/georss")
			.set("gml", "http://www.opengis.net/gml").set("gphoto", "http://schemas.google.com/photos/2007")
			.set("media", "http://search.yahoo.com/mrss/").set("openSearch", "http://a9.com/-/spec/opensearch/1.1/")
			.set("xml", "http://www.w3.org/XML/1998/namespace")
			.set("issues", "http://schemas.google.com/projecthosting/issues/2009");

	public ProjectHostingNetClient() {
		super();
		final ApacheHttpTransport transport = new ApacheHttpTransport();
		httpRequestFactory = transport.createRequestFactory(new HttpRequestInitializer() {

			@Override
			public void initialize(HttpRequest request) throws IOException {
				AtomParser parser = new AtomParser();
				parser.namespaceDictionary = NAMESPACE_DICTIONARY;
				request.addParser(parser);
				request.enableGZipContent = true;
			}
		});
	}

	public void setAuthToken(final String authToken) {
		this.authToken = authToken;
	}

	public IssueOverviewFeed getIssues(final Uri uri, final int startPosition, final int batchSize,
			final String cannedQuery) throws IOException {
		Log.d(LOG_TAG, "Getting issues for : " + uri);

		final GoogleUrl url = new GoogleUrl(uri.toString());
		url.put("fields", "link,openSearch:totalResults,entry(title,id)");
		url.put("start-index", startPosition);
		url.put("max-results", batchSize);
		if (cannedQuery != null) {
			url.put("can", cannedQuery);
		}
		final HttpRequest request = buildGetRequest(url);
		final HttpResponse httpResponse = request.execute();

		final IssueOverviewFeed overviewFeed = httpResponse.parseAs(IssueOverviewFeed.class);
		return overviewFeed;
	}

	@Override
	public IssueDetail getIssue(final Uri uri) throws IOException {
		GoogleUrl url = new GoogleUrl(uri.toString());
		HttpRequest request = buildGetRequest(url);
		HttpResponse httpResponse = request.execute();
		final IssueDetail issueDetail = httpResponse.parseAs(IssueDetail.class);
		return issueDetail;
	}

	/**
	 * @return
	 * @throws IOException
	 */
	private HttpRequest buildGetRequest(final GenericUrl url) throws IOException {
		HttpRequest request = this.httpRequestFactory.buildGetRequest(url);
		return request;
	}

	@Override
	public CommentFeed getComments(Uri uri, int startPosition, final int batchSize) throws IOException {
		Log.d(LOG_TAG, "Getting issues for : " + uri);

		final GoogleUrl url = new GoogleUrl(uri.toString());
		url.put("fields", "link,openSearch:totalResults,entry(title,content,published)");
		url.put("start-index", startPosition);
		url.put("max-results", batchSize);
		final HttpRequest request = buildGetRequest(url);
		final HttpResponse httpResponse = request.execute();
		final CommentFeed commentFeed = httpResponse.parseAs(CommentFeed.class);
		return commentFeed;
	}
}
